package com.androidlongs.androidpoptimeandoptionlibrary.common;

import android.app.*;
import android.content.*;
import android.util.*;
import android.widget.*;

import com.androidlongs.androidpoptimeandoptionlibrary.listener.*;
import com.androidlongs.androidpoptimeandoptionlibrary.view.*;
import com.androidlongs.androidpoptimeandoptionlibrary.view.TimePickerView.*;

import java.util.*;

import static com.androidlongs.androidpoptimeandoptionlibrary.activity.TestPickViewMainActivity.*;

/**
 * Created by androidlongs on 17/7/27.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

/**
 * 时间选择器
 */
public class CommonTimePop {
    private OptionsPickerView mPvOptions;
    private TimePickerView mPvTime;

    private CommonTimePop (){

    }
    private static  class  SingleCommonTimePop{
        private static CommonTimePop sCommonTimePop = new CommonTimePop();
    }

    public static CommonTimePop getinstance(){
        return SingleCommonTimePop.sCommonTimePop;
    }


    private Context mContext;

    /**
     * 在使用本库前 先要将城市相关数据拷贝到手机中
     */
    public void initFunction(Context context){
        mContext = context;
    }

    private CommonTimeSelectListener mCommonTimeSelectListener;
    public void showTimeViewFunction(CommonTimeSelectListener listener){
        this.mCommonTimeSelectListener = listener;
       if (mContext instanceof Activity){
           showTimeViewFunction();
       }else{
           Toast.makeText(mContext, "context not activity instanceof ", Toast.LENGTH_SHORT).show();
       }

    }
    private void showTimeViewFunction(){
        // 时间选择器
        mPvTime = new TimePickerView(mContext, Type.YEAR_MONTH_DAY);
        // 控制时间范围
//		 Calendar calendar = Calendar.getInstance();
//		 pvTime.setRange(calendar.get(Calendar.YEAR) - 20,
//		 calendar.get(Calendar.YEAR));
        mPvTime.setTime(new Date());
        mPvTime.setCyclic(false);
        mPvTime.setCancelable(true);
        // 时间选择后回调
        mPvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener()
        {

            @Override
            public void onTimeSelect(Date date)
            {
                String formatTime = getTime(date);
                log(formatTime);
                if (mCommonTimeSelectListener != null) {
                    mCommonTimeSelectListener.selectTime(date,formatTime);
                }
            }
        });
        // 弹出时间选择器
        mPvTime.show();

    }



    private void log(String msg){
        Log.e("time_pop","|——————————————————————————————————————start——————————————————————————————————————————————————");
        Log.d("time_pop","|      "+msg);
        Log.e("time_pop","|——————————————————————————————————————end——————————————————————————————————————————————————");

    }
}
